package cloud.seata.service.impl;

import cloud.seata.dao.OrderDao;
import cloud.seata.domain.Order;
import cloud.seata.service.AccountService;
import cloud.seata.service.OrderService;
import cloud.seata.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao dao;

    @Autowired
    private AccountService account;

    @Autowired
    private StorageService storage;


    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("--------------开始创建订单");
        dao.create(order);
        log.info("--------------订单微服务开始调用库存，做扣减");
        storage.decrease(order.getUserId(),order.getCount());
        log.info("--------------订单微服务开始调用账户，做扣减");
        account.decrease(order.getProductId(),order.getMoney());

        //修改订单状态
        log.info("--------------修改订单状态");
        dao.update(order.getUserId(),0);

        log.info("--------------订单结束");
    }
}
